home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / radio / checkradio.py < prev    next >
Text File  |  1994-08-01  |  9KB  |  340 lines

  1. # /***********************************************************
  2. # Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
  3. # Amsterdam, The Netherlands.
  4. #                         All Rights Reserved
  5. # Permission to use, copy, modify, and distribute this software and its 
  6. # documentation for any purpose and without fee is hereby granted, 
  7. # provided that the above copyright notice appear in all copies and that
  8. # both that copyright notice and this permission notice appear in 
  9. # supporting documentation, and that the names of Stichting Mathematisch
  10. # Centrum or CWI not be used in advertising or publicity pertaining to
  11. # distribution of the software without specific, written prior permission.
  12. # STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  13. # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  14. # FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  15. # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  18. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. # ******************************************************************/
  20.  
  21. # Continuously check radio transmissions on one or more ports.
  22. # After an idea of Behr de Ruiter.
  23. #
  24. # usage: checkradio [-t] [port] ...
  25. #
  26. # Ports are given as command line arguments, default is radio's default.
  27. # Shorthands 1..99 can be used as for radio's -p argument.
  28. #
  29. # With the -t option, repeatedly print status for each port argument.
  30. # Without -t, pop up a GL window displaying the CD file if there is noise.
  31.  
  32. # For best results, use /ufs/guido/bin/sgi/python to execute this.
  33. # Don't make the file executable; then dynamic loading of audioop fails!
  34.  
  35. # XXX To do:
  36. # - need an option to suppress looping when using -t
  37. # - need a `status only' option that sets exit status only
  38. # - DELAY and LOOP should be under control of command line options
  39. # - add options to specify font, colors and so on
  40. # - optionally tune radio to the first station transmitting noise
  41. # - should listen to info packets instead
  42. # - move the symbolic constants and some subroutines to separate modules
  43.  
  44.  
  45. import sys
  46. import socket
  47. import audioop
  48. import string
  49. import time
  50. import os
  51. from stat import *
  52. from SOCKET import *
  53. import getopt
  54.  
  55.  
  56. # Parametrizations
  57.  
  58. CTL_PORT = 54319            # control port
  59. PORT_OFFSET = 54320            # port offset if 1 <= port <= 99
  60. DEF_PORT = 54321            # default port (if no args)
  61. LOOP = 15                # listen for this many tenths seconds
  62. DELAY = 10                # seconds between successive tries
  63. LIMIT = 256                # silence threshold
  64. BUFSIZE = 1500                # read buffer size
  65.  
  66.  
  67. # Status constants returned by checkport()
  68.  
  69. TUNED = 'already tuned in'
  70. BINDFAILURE = 'bind failure'
  71. DEAD = 'not transmitting'
  72. SILENT = 'transmitting silence'
  73. NOISY = 'transmitting'
  74.  
  75.  
  76. # Main program
  77.  
  78. def main():
  79.     try:
  80.         optlist, args = getopt.getopt(sys.argv[1:], 'tx:y:')
  81.     except getopt.error:
  82.         sys.stdout = sys.stderr
  83.         print 'usage: checkradio',
  84.         print '[-x xorg] [-y yorg] [-t] [port] ...'
  85.         print '-x xorg, -y yorg: left top origin of window',
  86.         print '(negative values: right bottom)'
  87.         print '-t: tty mode output (looping), no window'
  88.         sys.exit(2)
  89.     #
  90.     do_win = 1
  91.     x, y = 140, 4
  92.     for opt, arg in optlist:
  93.         if opt == '-t':
  94.             do_win = 0
  95.         elif opt == '-x':
  96.             x = int(eval(arg))
  97.         elif opt == '-y':
  98.             y = int(eval(arg))
  99.     #
  100.     ports = []
  101.     for arg in args:
  102.         p = int(eval(arg))
  103.         if 1 <= p <= 99:
  104.             p = p + PORT_OFFSET
  105.         ports.append(p)
  106.     if not ports:
  107.         ports.append(DEF_PORT)
  108.     #
  109.     if do_win:
  110.         wincode(ports, (x, y))
  111.     else:
  112.         ttycode(ports)
  113.  
  114.  
  115. # Code for tty version
  116.  
  117. def ttycode(ports):
  118.     while 1:
  119.         for p in ports:
  120.             print 'port', p, ':',
  121.             sys.stdout.flush()
  122.             status, sender = checkport(p)
  123.             if status in (NOISY, SILENT):
  124.                 cdname = getinfostring(p, sender)
  125.                 if cdname:
  126.                     status = status + ': ' + cdname
  127.             print status
  128.         time.sleep(DELAY)
  129.  
  130. def getinfostring(port, sender):
  131.     name, port, transmitting, logfile, age, contents = \
  132.         getinfo(port, sender)
  133.     if 0 <= age < 99999:
  134.         contents = contents + ' (' + formatage(age) + ')'
  135.     return contents
  136.  
  137. def formatage(age):
  138.     if age < 60: return `age` + ' sec'
  139.     if age < 3600: return `age/60` + ' min'
  140.     if age < 24*3600: return `age/3600` + ' hrs'
  141.     return `age/(24*3600)` + ' days'
  142.  
  143.  
  144. # Code for GL window version
  145.  
  146. # Parameters
  147. timer_rate = 60 # Seconds
  148. color_choices = [95, 94, 93, 92, 91, 90, 89, 88]
  149.  
  150. def wincode(ports, org):
  151.     if len(ports) > 1:
  152.         sys.stderr.write('warning: only the first port arg is used\n')
  153.     port = ports[0]
  154.     import gl, GL, DEVICE, fm
  155.     #gl.foreground()
  156.     fh = fm.findfont('Helvetica').scalefont(8)
  157.     fh.setfont()
  158.     str, age = getinfopair(port)
  159.     # Always create the window initially -- to initialize gl
  160.     wid = createwin(org, fh, str)
  161.     gl.qdevice(DEVICE.TIMER1)
  162.     gl.noise(DEVICE.TIMER1, timer_rate*60) # 60th of a second
  163.     while 1:
  164.         dev, val = gl.qread()
  165.         if dev == DEVICE.REDRAW:
  166.             redraw_window(wid, str, age)
  167.         elif dev == DEVICE.TIMER1:
  168.             oldstr = str
  169.             str, age = getinfopair(port)
  170.             if str <> oldstr:
  171.                 if wid > 0:
  172.                     org = currentorg(org)
  173.                     deletewin(wid)
  174.                     wid = -1
  175.                 if str:
  176.                     wid = createwin(org, fh, str)
  177.             elif wid > 0:
  178.                 redraw_window(wid, str, age)
  179.  
  180. def currentorg((oldx, oldy)):
  181.     import gl, GL
  182.     x, y = gl.getorigin()
  183.     if oldx >= 0 and oldy >= 0: return (oldx, oldy)
  184.     xsize, ysize = gl.getsize()
  185.     xmax = gl.getgdesc(GL.GD_XPMAX)
  186.     ymax = gl.getgdesc(GL.GD_YPMAX)
  187.     if oldx < 0:
  188.         x = (x + xsize) - (xmax+1)
  189.         if x >= 0: x = -1
  190.     if oldy < 0:
  191.         y = (y + ysize) - (ymax+1)
  192.         if y >= 0: y = -1
  193.     return x, y
  194.  
  195. def redraw_window(wid, str, age):
  196.     import gl, GL, fm
  197.     mins = age/60 # Convert to minutes
  198.     
  199.     gl.color(color_choices[min(max(0, mins/9), len(color_choices)-1)])
  200.     gl.clear()
  201.     
  202.     gl.color(GL.BLACK)
  203.  
  204.     xsize, ysize = gl.getsize()
  205.     gl.bgnclosedline()
  206.     gl.v2i(0, 0)
  207.     gl.v2i(0, ysize-1)
  208.     gl.v2i(xsize-1, ysize-1)
  209.     gl.v2i(xsize-1, 0)
  210.     gl.endclosedline()
  211.  
  212.     gl.cmov2i(4, 4)
  213.     fm.prstr(str)
  214.  
  215. def createwin((x, y), fh, str):
  216.     import gl, GL, DEVICE
  217.     gl.noborder()
  218.     xsize = fh.getstrwidth(str) + 7
  219.     ysize = 16
  220.     if x < 0 or y < 0:
  221.         xmax = gl.getgdesc(GL.GD_XPMAX)
  222.         ymax = gl.getgdesc(GL.GD_YPMAX)
  223.         if x < 0: x = x + (xmax+1) - xsize
  224.         if y < 0: y = y + (ymax+1) - ysize
  225.     gl.prefposition(x, x + xsize, y, y + ysize)
  226.     wid = gl.winopen('checkradio')
  227.     gl.qenter(DEVICE.REDRAW, wid)
  228.     return wid
  229.  
  230. def deletewin(wid):
  231.     import gl, GL, DEVICE
  232.     gl.winclose(wid)
  233.  
  234. def getinfopair(port):
  235.     state, sender = checkport(port)
  236.     if state not in (NOISY, SILENT):
  237.         return '', 99999
  238.     name, port, transmitting, logfile, age, contents = \
  239.         getinfo(port, sender)
  240.     return contents, age
  241.  
  242.  
  243. # Common code
  244.  
  245. def checkport(port):
  246.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  247.     try:
  248.         s.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
  249.     except socket.error:
  250.         print 'warning: cannot set socket option SO_REUSEPORT'
  251.     try:
  252.         s.bind('', port)
  253.     except socket.error, msg:
  254.         if msg == (114, 'Address already in use'):
  255.             return TUNED, msg
  256.         else:
  257.             return BINDFAILURE, msg
  258.     transmitting = 0
  259.     noise = 0
  260.     sender = None
  261.     for i in range(LOOP):
  262.         if i: time.millisleep(100)
  263.         while s.avail():
  264.             data, sender = s.recvfrom(BUFSIZE)
  265.             if data[:6] == 'radio:': continue
  266.             transmitting = 1
  267.             lindata = audioop.ulaw2lin(data, 2)
  268.             n = audioop.max(lindata, 2)
  269.             if n > LIMIT:
  270.                 noise = n
  271.                 break
  272.         if noise: break
  273.     s.close()
  274.     if not transmitting:
  275.         return DEAD, sender
  276.     if not noise:
  277.         return SILENT, sender
  278.     return NOISY, sender
  279.  
  280. def getinfo(port, sender):
  281.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  282.     s.sendto('radio:s', (sender[0], CTL_PORT))
  283.     for i in range(LOOP):
  284.         time.millisleep(100)
  285.         while s.avail():
  286.             data, realsender = s.recvfrom(BUFSIZE)
  287.             if data[:7] == 'radio:S':
  288.                 return decodeinfo(data)
  289.  
  290. def decodeinfo(data):
  291.     fields = string.splitfields(data, ':')
  292.     name = fields[2]
  293.     port = eval(fields[3])
  294.     if fields[4:]:
  295.         transmitting = eval(fields[4])
  296.         logfile = fields[5]
  297.         age = eval(fields[6])
  298.         contents = string.joinfields(fields[7:], ':')
  299.     else:
  300.         transmitting = -1
  301.         programfile = '/ufs/' + name + '/CD'
  302.         logfile = programfile + 'log'
  303.         age = getage(programfile)
  304.         if age == None:
  305.             age = -1
  306.         contents = getcontents(programfile)
  307.         if contents == None:
  308.             contents = '???'
  309.     return name, port, transmitting, logfile, age, contents
  310.     return None
  311.  
  312. def getcontents(filename):
  313.     try:
  314.         f = open(filename, 'r')
  315.     except IOError:
  316.         return None
  317.     res = f.readline()
  318.     f.close()
  319.     return string.strip(res)
  320.  
  321. def getage(filename):
  322.     try:
  323.         st = os.stat(filename)
  324.     except os.error:
  325.         return None
  326.     return time.time() - st[ST_MTIME]
  327.  
  328.  
  329. # Call the main program
  330.  
  331. try:
  332.     main()
  333. except KeyboardInterrupt:
  334.     print
  335.     print '[Interrupt]'
  336.